package com.easyaccount.controller;

import com.easyaccount.bean.RoleMenuTree;
import com.easyaccount.commons.CommonResp;
import com.easyaccount.commons.TablePage;
import com.easyaccount.model.Menu;
import com.easyaccount.model.Role;
import com.easyaccount.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @RequestMapping("list")
    public TablePage list(TablePage tablePage){
        return roleService.searchByName(tablePage, null);
    }

    @RequestMapping("all")
    public CommonResp all(){
        return roleService.list();
    }

    @RequestMapping("menuList")
    public CommonResp menuList(@RequestParam("roleId") String roleId){
        return roleService.roleMenuLists(Integer.valueOf(roleId));
    }

    @RequestMapping("search")
    public TablePage search(TablePage tablePage,@RequestParam("roleName") String roleName){
        TablePage result = roleService.searchByName(tablePage, roleName);
        return result;
    }

    @RequestMapping("add")
    public CommonResp add(Role role,@RequestParam(value = "selected[]",required = false) List<Integer> selected){
        System.out.println(role);
        System.out.println(selected);
        return roleService.save(role,selected);
    }

    @RequestMapping("update")
    public CommonResp update(Role role,@RequestParam(value = "selected[]",required = false) List<Integer> selected){
        System.out.println(role);
        System.out.println(selected);
        return roleService.update(role,selected);
    }

    @RequestMapping("delete")
    public CommonResp delete(Role Role){
        return roleService.delete(Role);
    }


}
